﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace TZReport_Util.Util
{
    public sealed class JsonXmlUtils
    {
        #region Xml和Json互转
        public static string XmlToJson(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            string jsonContent = JsonConvert.SerializeXmlNode(doc);
            return jsonContent;
        }

        public static string JsonToXml(string json)
        {
            XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
            string result = ConvertXmlToString(doc);
            return result;
        }

        public static string ConvertXmlToString(XmlDocument xmlDoc)
        {
            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, null);
            writer.Formatting = System.Xml.Formatting.Indented;
            xmlDoc.Save(writer);
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            stream.Position = 0;
            string xmlString = sr.ReadToEnd();
            sr.Close();
            stream.Close();
            return xmlString;
        }
        #endregion

        #region Xml和实体类互转
        public static string XmlSerialize<T>(T obj)
        {
            using (StringWriter sw = new StringWriter())
            {
                Type t = obj.GetType();
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
                serializer.Serialize(sw, obj);
                sw.Close();
                return sw.ToString();
            }
        }

        public static T DESerializer<T>(string strXML) where T : class
        {
            try
            {
                using (StringReader sr = new StringReader(strXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return serializer.Deserialize(sr) as T;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }
        }

        public static T DeserializeXML<T>(string xml) where T : class, new()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            T t = new T();
            PropertyInfo[] props = t.GetType().GetProperties();
            foreach (PropertyInfo p in props)
            {
                XmlNode xmlNode = xmlDoc.SelectSingleNode($"xml/{ p.Name}");
                string pVal = xmlNode.InnerText;

                if (p.PropertyType == typeof(Int32))
                    p.SetValue(t, int.Parse(pVal));
                else if (p.PropertyType == typeof(DateTime))
                    p.SetValue(t, DateTime.Parse(pVal));
                else if (p.PropertyType == typeof(Guid))
                    p.SetValue(t, Guid.Parse(pVal));
                else if (p.PropertyType == typeof(Int64))
                    p.SetValue(t, long.Parse(pVal));
                else
                    p.SetValue(t, pVal);
            }

            return t;
        }
        #endregion

        #region Json和实体互转
        public static string SerializeObject(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public static T DeserializeObject<T>(string content)
        {
            return JsonConvert.DeserializeObject<T>(content);
        }
        #endregion
    }
}
